package dao;

import conn.ConData;
import model.Meetingmanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MeetingmanageDao {
    Connection con=null;//连接数据库对象  默认值为空
    ConData conn=new ConData();//连接数据库驱动

    /**
     * 给meetingmanage表添加数据
     * @param meetingmanage 实体
     * @return
     */
    public int add(Meetingmanage meetingmanage){//参数类型 参数名  向方法里传参
        int num=0;//添加不成功
        //创建sql语句
        String sql="INSERT INTO meetingmanage (title,creater,maintainmain,place,participant,accessory) VALUE(?,?,?,?,?,?)";
        //创建执行sql的对象A
        PreparedStatement ps;//带参数的执行sql语句对象
        try {
            //输出meetingmanage的创建人方法的值
            System.out.println(meetingmanage.getCreater());
            //调用连接数据库的方法
            con=conn.getCon();
            //返回一个PreparedStatement实例
            ps=con.prepareStatement(sql);


            ps.setString(1 , meetingmanage.getTitle());
            ps.setString(2, meetingmanage.getCreater());
           /* ps.setString(4 , meetingmanage.getType());*/
            ps.setString(3 , meetingmanage.getMaintainmain());
            ps.setString(4 , meetingmanage.getPlace());
            ps.setString(5 , meetingmanage.getParticipant());
            ps.setString(6 , meetingmanage.getAccessory());

            System.out.println(meetingmanage.getTitle());
            System.out.println(meetingmanage.getCreater());
            System.out.println(meetingmanage.getMaintainmain());
            System.out.println(meetingmanage.getPlace());
            System.out.println(meetingmanage.getParticipant());
            System.out.println(meetingmanage.getAccessory());
            num=ps.executeUpdate();//执行sql语句
            //关闭执行对象
            ps.close();
            con.close();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return num;
    }



    /**
     * 根据主键更新信息
     * @param meetingmanage
     * @return
     */

    public int update(Meetingmanage meetingmanage){
        int isSuccess=0;//修改未成功
        String sql="UPDATE meetingmanage SET creater=? WHERE meetingmanageid=?";//修改语句
        /*title=?,creater=?,maintainmain=?,place=?,participant=?,accessory=?*/
        PreparedStatement ps;
        try{
            //调用连接数据库的方法
            con=conn.getCon();
            //返回一个PrepareStatement实例
            ps=con.prepareStatement(sql);

           /* //时间格式转换
            java.util.Date maintaintime=meetingmanage.getMaintaintime();
            java.sql.Date Maintaintime=new java.sql.Date(maintaintime.getTime());
            java.util.Date createtime=meetingmanage.getCreatetime();
            java.sql.Date Createtime=new java.sql.Date(createtime.getTime());*/


           /* ps.setString(1 , meetingmanage.getTitle());*/
          /*  ps.setDate(2 , Maintaintime);
            ps.setDate(3 , Createtime);*/
            ps.setString(1 , meetingmanage.getCreater());
            /*ps.setString(3 , meetingmanage.getType());*/
           /* ps.setString(3 , meetingmanage.getMaintainmain());
            ps.setString(4 , meetingmanage.getPlace());
            ps.setString(5 , meetingmanage.getParticipant());
            ps.setString(6 , meetingmanage.getAccessory());*/
            ps.setInt(2 , meetingmanage.getMeetingmanageid());

            //执行修改
            isSuccess=ps.executeUpdate();
            //关闭执行对象
            ps.close();
            con.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return isSuccess;
    }


    /*
     * 根据主键删除信息
     * */

    public int del(Meetingmanage meetingmanage){
        int isDel=0;//删除未成功
        //创建执行sql语句
        String sql="DELETE FROM meetingmanage WHERE meetingmanageid=?";
        //创建执行sql的对象
        PreparedStatement ps;
        try{
            con=conn.getCon();
            ps=con.prepareStatement(sql);
            //设置删除条件
            ps.setInt(1, meetingmanage.getMeetingmanageid());
            //删除
            isDel=ps.executeUpdate();
            //关闭执行对象
            ps.close();
            //关闭连接对象
            con.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return isDel;
    }



    /*
     * 根据查询条件返回查询结果
     *
     * */
    public List<Meetingmanage> getAll(Meetingmanage meetingmanage){
        System.out.println("进入查询方法");
        List<Meetingmanage> listMeetingmanage=new ArrayList<Meetingmanage>();
        //创建sql字符串
        String str="SELECT * FROM meetingmanage ";
        int Meetingmanageid=0;
        con=conn.getCon();
        try{
            //创建执行sql的对象
            PreparedStatement ps=con.prepareStatement(str);
            System.out.println("查询语句:"+str);
            //设置查询条件
//			ps.setInt(1, Meetingmanageid);
            //返回查询记录结果的记录集
            ResultSet rs=ps.executeQuery();

            //将记录集存入集合当中
            while(rs.next()){
                //声明一个实体对象
                Meetingmanage b=new Meetingmanage();
                //给对象赋值
                b.setMeetingmanageid(rs.getInt(1));
                b.setTitle(rs.getString(2));
                b.setMaintaintime(rs.getString(3));
                b.setCreatetime(rs.getString(4));
                b.setCreater(rs.getString(5));
                b.setType(rs.getString(6));
                b.setMaintainmain(rs.getString(7));
                b.setPlace(rs.getString(8));
                b.setParticipant(rs.getString(9));
                b.setAccessory(rs.getString(10));
                //将对象实体添加到集合中
                listMeetingmanage.add(b);

            }
            //关闭记录集
            rs.close();
            //关闭执行的sql对象
            ps.close();
            //关闭连接
            con.close();

        }catch(SQLException e){
            e.printStackTrace();
        }
        System.out.println(listMeetingmanage);
        return listMeetingmanage;
    }


//单项查询
    public List<Meetingmanage> getDan(Meetingmanage meetingmanage){
        System.out.println("进入单项查询方法");
        List<Meetingmanage> listMeetingmanage=new ArrayList<Meetingmanage>();
        //创建sql字符串
        String str="SELECT * FROM meetingmanage WHERE creater=?";
        con=conn.getCon();
        try{
            //创建执行sql的对象
            PreparedStatement ps=con.prepareStatement(str);
            System.out.println("查询语句:"+str);
            //设置查询条件
			ps.setString(1, meetingmanage.getCreater());
            //返回查询记录结果的记录集
            ResultSet rs=ps.executeQuery();

            //将记录集存入集合当中
            while(rs.next()){
                //声明一个实体对象
                Meetingmanage b=new Meetingmanage();
                //给对象赋值
                b.setMeetingmanageid(rs.getInt(1));
                b.setTitle(rs.getString(2));
                b.setMaintaintime(rs.getString(3));
                b.setCreatetime(rs.getString(4));
                b.setCreater(rs.getString(5));
                /*b.setType(rs.getString(6));*/
                b.setMaintainmain(rs.getString(6));
                b.setPlace(rs.getString(7));
                b.setParticipant(rs.getString(8));
                b.setAccessory(rs.getString(9));
                //将对象实体添加到集合中
                listMeetingmanage.add(b);

            }
            //关闭记录集
            rs.close();
            //关闭执行的sql对象
            ps.close();
            //关闭连接
            con.close();

        }catch(SQLException e){
            e.printStackTrace();
        }
        System.out.println(listMeetingmanage);
        return listMeetingmanage;
    }


//更改中根据ID进行查询
    public List<Meetingmanage> getAddc(Meetingmanage meetingmanage){
        System.out.println("根据ID进行查询");
        List<Meetingmanage> listMeetingmanage=new ArrayList<Meetingmanage>();
        //创建sql字符串
        String str="SELECT * FROM meetingmanage WHERE meetingmanageid=?";
        con=conn.getCon();
        try{
            //创建执行sql的对象
            PreparedStatement ps=con.prepareStatement(str);
            System.out.println("查询语句:"+str);
            //设置查询条件
            ps.setInt(1, meetingmanage.getMeetingmanageid());
            //返回查询记录结果的记录集
            ResultSet rs=ps.executeQuery();

            //将记录集存入集合当中
            while(rs.next()){
                //声明一个实体对象
                Meetingmanage b=new Meetingmanage();
                //给对象赋值
                b.setMeetingmanageid(rs.getInt(1));
                b.setTitle(rs.getString(2));
                b.setMaintaintime(rs.getString(3));
                b.setCreatetime(rs.getString(4));
                b.setCreater(rs.getString(5));
               /* b.setType(rs.getString(6));*/
                b.setMaintainmain(rs.getString(6));
                b.setPlace(rs.getString(7));
                b.setParticipant(rs.getString(8));
                b.setAccessory(rs.getString(9));
                //将对象实体添加到集合中
                listMeetingmanage.add(b);

            }
            //关闭记录集
            rs.close();
            //关闭执行的sql对象
            ps.close();
            //关闭连接
            con.close();

        }catch(SQLException e){
            e.printStackTrace();
        }
        System.out.println(listMeetingmanage);
        return listMeetingmanage;
    }
}
